<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class DianzanController extends Controller
{
     public function add($id)
     {
         if(Session('user_info'))
         {
             $isdian = DB::table('dianzan') -> where([['uid',Session('user_info') -> id],['fid',$id]]) -> first();
             if($isdian)
             {

                 if($isdian -> num == 1)
                 {
                      $data['num'] = 0;
                      $res = DB::table('dianzan') -> where([['uid',Session('user_info') -> id],['fid',$id]]) -> update($data);
                 }else
                 {
                     $data['num'] = 1;
                     $res = DB::table('dianzan') -> where([['uid',Session('user_info') -> id],['fid',$id]]) -> update($data);
                 }
                 $dian = DB::table('dianzan') -> where([['fid',$id]]) -> get();
                 $num = '';
                 foreach($dian as $row)
                 {
                    $num += $row -> num;
                 }
                       return $num;
             }else
             {
                 $data['num'] = 1;
                 $data['uid'] = Session('user_info') -> id;
                 $data['fid'] = $id;
                 $time = Date('Y:m:d H:i:s');
                 $data['created_at'] = $time;
                 $data['updated_at'] = $time;
                 $res = DB::table('dianzan') -> insertGetId($data);
                 $dian = DB::table('dianzan') -> where([['fid',$id]]) -> get();
                 $num = '';
                 foreach($dian as $row)
                 {
                    $num += $row -> num;
                 }
                       return $num;
             }
         }else
         {
              return '请您登录';
         }


     }
}
